package queue;

/**
 * @author tongchen
 * @create 2022-10-18 10:14
 */
public class MyQueue_DoubleLinkedList {
    public static void main(String[] args) {
        MyQueue_DoubleLinkedList myQueue_doubleLinkedList = new MyQueue_DoubleLinkedList();
        myQueue_doubleLinkedList.offer(1);
        myQueue_doubleLinkedList.offer(2);
        myQueue_doubleLinkedList.offer(3);
        myQueue_doubleLinkedList.offer(4);
        System.out.println(myQueue_doubleLinkedList.peek());
        System.out.println(myQueue_doubleLinkedList.size);
        System.out.println(myQueue_doubleLinkedList.isEmpty());
        myQueue_doubleLinkedList.display();
        myQueue_doubleLinkedList.poll();
        myQueue_doubleLinkedList.poll();
        System.out.println(myQueue_doubleLinkedList.peek());
        myQueue_doubleLinkedList.poll();
        myQueue_doubleLinkedList.poll();
        System.out.println("******************");
        myQueue_doubleLinkedList.display();
        System.out.println(myQueue_doubleLinkedList.size);
        System.out.println(myQueue_doubleLinkedList.isEmpty());
        System.out.println(myQueue_doubleLinkedList.peek());
    }
    DouleLinkedList head;
    DouleLinkedList tail;
    int size;
    public void offer(int e){
        DouleLinkedList douleLinkedList = new DouleLinkedList(e);
        if(head==null){
            head=tail=douleLinkedList;
        }
        else{
            tail.next=douleLinkedList;
            douleLinkedList.pre=tail;
        }
        tail=douleLinkedList;
        size++;
    }
    public int poll(){
        //用头结点进行出队列操作
        if(head==null){
         throw new RuntimeException("队列为空，无法出队列操作");
        }
        int value= head.val;
        if (head.next!=null){
            head=head.next;
            head.pre.next=null;
            head.pre=null;
        }
        else{
            head=null;
            tail=null;
        }
        size--;
        return value;
    }
    public int peek(){
        if (head==null){
            throw new RuntimeException("链表元素为空");
        }
        return head.val;

    }
    public int size() {
        return size;

    }
    //对队列进行遍历
    public void display(){
        StringBuilder sb=new StringBuilder();
        sb.append('[');
        DouleLinkedList cur=head;
        while(cur!=null){
            sb.append(cur);
            if (cur.next!=null){
                sb.append(',');
            }
            cur=cur.next;
        }
        sb.append(']');
        System.out.println(sb);
    }
    public boolean isEmpty(){
        if (head==null){
            return true;
        }
        return false;

    }
}
class DouleLinkedList{
    int val;
    DouleLinkedList pre;
    DouleLinkedList next;

    public DouleLinkedList(int val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return "DouleLinkedList{" +
                "val=" + val +
                '}';
    }
}
